package com.cy;

import com.cy.pj.sys.service.realm.ShiroRealm;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.mgt.RememberMeManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;


@EnableCaching//启动springboot工程中的内置缓存
@SpringBootApplication
public class AdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminApplication.class,args);
    }
    @Bean
    public Realm realm() {//org.apache.shiro.realm.Realm
        return new ShiroRealm();
    }
    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
        //过滤链对象的定义(这个过滤链中包含了很多内置过滤器)
        DefaultShiroFilterChainDefinition chainDefinition =
                new DefaultShiroFilterChainDefinition();
        //指定过滤链中的过滤规则,例如：
        //配置/user/login/**开头的资源，可以匿名访问(不用登录就可以访问),其中anon为shiro框架指定的匿名过滤器
        chainDefinition.addPathDefinition("/user/login/**","anon");
        //配置登出操作，logout为shiro提供的一个默认登出过滤器
        chainDefinition.addPathDefinition("/user/logout","logout");
        //配置以/**开头的资源必须都要经过认证，其中authc为shiro框架指定的认证过滤器
//        chainDefinition.addPathDefinition("/**", "authc");
        //假如配置了记住我功能，则需将authc改为user
        chainDefinition.addPathDefinition("/**", "user");
        return chainDefinition;
    }
    //配置session容器管理对象
    @Bean
    public SessionManager sessionManager(){
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        //设置延迟时间
        sessionManager.setGlobalSessionTimeout(5*60*1000);//延迟时间5分钟
        //删除无效session
        sessionManager.setDeleteInvalidSessions(true);
        //当客户端cookie被禁用是是否重写Url
        sessionManager.setSessionIdUrlRewritingEnabled(false);
        return sessionManager;
    }
    @Bean
    public RememberMeManager rememberMeManager(){
        CookieRememberMeManager cManager = new CookieRememberMeManager();
        SimpleCookie cookie = new SimpleCookie("remeberCGB");
        cookie.setMaxAge(7*24*60*60);//7天
        cManager.setCookie(cookie);
        //设置加密解密密钥
        //cManager.setCipherKey(Base64.decode(""));
        return cManager;
    }
    /**
     * 配置Shiro中自带的CacheManager对象，此对象可以通过Cache缓存授权时获取到权限信息，下次再访问
     * 授权方法时不需要再查用户权限，这样可以减少数据库的访问压力，同时提高授权性能
     *
     */
    @Bean
    protected CacheManager shiroCacheManager(){
        return new MemoryConstrainedCacheManager();
    }
    @Bean //<bean id="" class="">
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator proxyCreator=
                new DefaultAdvisorAutoProxyCreator();
        proxyCreator.setProxyTargetClass(true);//让目标对象上的注解映射是生效
        //proxyCreator.setUsePrefix(true);
        return proxyCreator;
    }
}
